package com.centit.dtjktxlog.controller;

import com.centit.airplanebaseinfo.po.AirplaneBaseinfo;
import com.centit.airplanebaseinfo.service.AirplaneBaseinfoManager;
import com.centit.dtjktxlog.po.JddtDtjktxlog;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.centit.dtjktxlog.service.JddtDtjktxlogManager;
	

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.centit.framework.common.JsonResultUtils;
import com.centit.framework.common.ResponseMapData;
import com.centit.framework.components.CodeRepositoryUtil;
import com.centit.framework.core.controller.BaseController;
import com.centit.framework.model.basedata.IRoleInfo;
import com.centit.framework.security.model.CentitUserDetails;
import com.centit.jksxgl.po.JddtJksxgl;
import com.centit.jksxgl.service.JddtJksxglManager;
import com.centit.support.database.utils.PageDesc;
import com.centit.util.LayuiResultUtil;
import com.centit.util.SendEmail;

/**
 * JddtDtjktxlog  Controller.
 * create by scaffold 2020-03-31 
 * @author 脚手架，改成开发者的名字
 * T_JDDT_DTJKTXLOGT_JDDT_DTJKTXLOG【动态监控提醒日志表】   
*/


@Controller
@RequestMapping("/dtjktxlog/jddtdtjktxlog")
public class JddtDtjktxlogController  extends BaseController {
	private static final Log log = LogFactory.getLog(JddtDtjktxlogController.class);
	
	@Resource
	private JddtDtjktxlogManager jddtDtjktxlogMag;	
	
	@Resource
	private AirplaneBaseinfoManager airplaneBaseinfoMag;
	
	@Resource
	private JddtJksxglManager jddtJksxglMag;
	
	SimpleDateFormat spf = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 查询所有   T_JDDT_DTJKTXLOG  列表
     *
     * @param field    json中只保存需要的属性名
     * @param request  {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     * @return {data:[]}
     */
    @RequestMapping(method = RequestMethod.GET)
    public void list(String[] field, PageDesc pageDesc, HttpServletRequest request, HttpServletResponse response) {
    	String limit = request.getParameter("limit");
        if (StringUtils.isBlank(limit)) {
            limit = "10";
        }
        pageDesc.setPageSize(Integer.parseInt(limit));
        Map<String, Object> searchColumn = convertSearchColumn(request);        
        JSONArray jsonArrayObjects = jddtDtjktxlogMag.listAsJSONArray(searchColumn, pageDesc);
        for(int i = 0;i<jsonArrayObjects.size(); i++) {
        	JSONObject obj = jsonArrayObjects.getJSONObject(i);
        	if (StringUtils.isNotEmpty(obj.getString("dqDate"))) {
        		obj.put("dqDate", spf.format(obj.getDate("dqDate")));
        	}
        	if (StringUtils.isNotEmpty(obj.getString("jcStatus"))) {
        		obj.put("jcStatusText", CodeRepositoryUtil.getValue("JC_STATUS", obj.getString("jcStatus")));
            }
            if (StringUtils.isNotEmpty(obj.getString("clStatus"))) {
            	obj.put("clStatusText", CodeRepositoryUtil.getValue("CL_STATUS", obj.getString("clStatus")));
            }
            if (StringUtils.isNotEmpty(obj.getString("txtype"))) {
            	obj.put("txtypeText", CodeRepositoryUtil.getValue("TXTYPE", obj.getString("txtype")));
            }
            if (StringUtils.isNotEmpty(obj.getString("nature"))) {
            	obj.put("nature", CodeRepositoryUtil.getValue("AIRXZ", obj.getString("nature")));
            }
        }
        ResponseMapData resData = new ResponseMapData();
        resData.addResponseData(OBJLIST, jsonArrayObjects);
        resData.addResponseData(PAGE_DESC, pageDesc);
        LayuiResultUtil.layuiwriteResponseDataAsJson(resData, pageDesc, response);
    }
    
    /**
     * 查询单个  T_JDDT_DTJKTXLOG 
	
	 * @param gcbsdjbs  GCBSDJBS
	 * @param jksxId  JKSX_ID
	 * @param clStatus  CL_STATUS
     *  
     * 
     * @param response    {@link HttpServletResponse}
     * @return {data:{}}
     */
    @RequestMapping(value = "/{recId}", method = {RequestMethod.GET})
    public ModelAndView getJddtDtjktxlog(@PathVariable String recId, HttpServletResponse response) {
    	ModelAndView mv = new ModelAndView();
    	JddtDtjktxlog jddtDtjktxlog = jddtDtjktxlogMag.getObjectById(recId);
    	AirplaneBaseinfo airplaneBaseinfo = airplaneBaseinfoMag.getObjectById(jddtDtjktxlog.getAirId());
    	JddtJksxgl jddtJksxgl = jddtJksxglMag.getObjectById(jddtDtjktxlog.getJksxId());
    	String forward = "forward:/modules/jdddtjktxlog/jdddtjktxlogView.jsp";
    	mv.addObject(OBJECT, jddtDtjktxlog);
    	mv.addObject("airplaneBaseinfo", airplaneBaseinfo);
    	mv.addObject("jddtJksxgl", jddtJksxgl);
		mv.setViewName(forward);
		return mv;
    }
    
    @RequestMapping(value = "/remind/{recId}", method = {RequestMethod.POST})
    public void remind(@PathVariable String recId,HttpServletResponse response) {
    	JddtDtjktxlog jddtDtjktxlog = jddtDtjktxlogMag.getObjectById(recId);
    	String txtype  = jddtDtjktxlog.getTxType();
    	if(txtype.equals("1")) {
			//短信提醒
			System.out.println("发送三证到期短信提醒");
		}
		if(txtype.equals("2")) {
			//邮件提醒
			try {
				SendEmail.sendEmail("您的飞机三证时间即将过期，请及时处理", "411527091@qq.com");
			} catch (Exception e) {
				System.out.println("邮件发送失败");
				e.printStackTrace();
			}
		}
		if(txtype.equals("3")) {
			//站内消息提醒
			
		}
		jddtDtjktxlog.setRgtxDate(new Date());
		jddtDtjktxlog.setRgtxTime((jddtDtjktxlog.getRgtxTime() == null?0:jddtDtjktxlog.getRgtxTime())+1);
		jddtDtjktxlogMag.updateObject(jddtDtjktxlog);
		JsonResultUtils.writeSingleDataJson("操作成功",response);
    }
    @RequestMapping(value = "/closeTx/{recId}", method = {RequestMethod.POST})
    public void closeTx(@PathVariable String recId,HttpServletResponse response) {
    	JddtDtjktxlog jddtDtjktxlog = jddtDtjktxlogMag.getObjectById(recId);
    	jddtDtjktxlog.setClStatus("2");
		jddtDtjktxlogMag.updateObject(jddtDtjktxlog);
		JsonResultUtils.writeSingleDataJson("操作成功",response);
    }
}
